<template>
  <div class="app-container">
    <!-- 定价设置-定价模板 -->
    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新建模板</el-button>
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
        >删除模板</el-button>
      </el-col>
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板名称" prop="templateName" min-width="150" />
      <el-table-column label="模板类型" prop="templateTypeStr" min-width="120" />
      <el-table-column label="状态" prop="status" min-width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.status==0" type="warning">禁用</el-tag>
          <el-tag v-else type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="200" class-name="small-padding">
        <template #default="scope">
          <el-button type="text" @click="updateRow(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="viewRow(scope.row)">详情</el-button>
          <el-button type="text" @click="changeStatus(scope.row)">{{ scope.row.status==0?'启用':'禁用' }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <!-- 编辑 -->
    <dialogModifyTemplate :props="dialogModifyProps" @submitForm="resetQuery" />
    <!-- 详情 -->
    <dialogViewTemplate :props="dialogViewProps" />
  </div>
</template>

<script setup name="PriceTemplate">
import { getCurrentInstance } from 'vue'
import { onMounted, reactive } from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import { changeTemplateStatus, delPriceTemplate, priceTemplateList } from '../../../../../api/ven'
import dialogModifyTemplate from './components/dialog/modifyTemplate'
import dialogViewTemplate from './components/dialog/viewTemplate'


const { proxy } = getCurrentInstance()

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    status: undefined
  },
  loading: false,
  tableData: [],
  total: 0,
  multiple: true
})
const { queryParams, loading, tableData, total, multiple } = toRefs(data)

let selectedIds = []
const dialogPropsBase = {
  open: false,
  title: '',
  data: null
}
const dialogModifyProps = reactive({
  ...dialogPropsBase
})
const dialogViewProps = reactive({
  ...dialogPropsBase
})


// 获取列表
function getList() {
  loading.value = true
  priceTemplateList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleSelectionChange(selection) {
  selectedIds = selection.map(item => item.id)
  multiple.value = !selection.length
}

function handleAdd() {
  _.assign(dialogModifyProps, dialogPropsBase, {
    open: true,
    title: '新建模板'
  })
}

function handleDelete(row) {
  let ids, title
  if (row.id) {
    ids = row.id
    title = `是否确认删除模板 ${row.templateName}`
  } else {
    ids = selectedIds
    title = `是否确认删除 ${selectedIds.length} 个模板？`
  }

  proxy.$modal.confirm(title).then(function() {
    return delPriceTemplate(ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  })
}

function updateRow(row) {
  _.assign(dialogModifyProps, dialogPropsBase, {
    open: true,
    title: '编辑模板',
    data: { ...row }
  })
}

function viewRow(row) {
  _.assign(dialogViewProps, dialogPropsBase, {
    open: true,
    title: '查看模板',
    data: { ...row }
  })
}

function changeStatus(row) {
  proxy.$modal
    .confirm(`确定操作？`)
    .then(() => {
      return changeTemplateStatus(row)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('操作成功')
    })
}

onMounted(() => {
  getList()
})

</script>

<style scoped>

</style>
